package com.cskaoyan.controller;

import com.cskaoyan.bean.BrandInfo;
import com.cskaoyan.bean.Issue;
import com.cskaoyan.bean.Keyword;
import com.cskaoyan.bo.*;
import com.cskaoyan.service.MallManagementService;
import com.cskaoyan.vo.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.lang.reflect.Array;
import java.util.List;

import javax.validation.Valid;



/**
 * @author LiaoLong
 * @date 2021-05-09 11:20
 */
@RestController
@RequestMapping("admin")
public class MallManagementController {

    @Autowired
    private MallManagementService mallManagementService;

    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    @GetMapping("keyword/list")
    public BaseRespVo keywordList(SearchKeywordBO searchKeywordBO){
        KeywordVO keywordVO = mallManagementService.getKeywords(searchKeywordBO);
        return BaseRespVo.ok(keywordVO);
    }

    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    @PostMapping("keyword/{op}")
    public BaseRespVo updateKeyword(@RequestBody Keyword keyword, @PathVariable String op){
        mallManagementService.updateKeyword(keyword, op);
        return BaseRespVo.ok();
    }

    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    @PostMapping("keyword/create")
    public BaseRespVo createKeyword(@RequestBody @Valid Keyword keyword){
        mallManagementService.createKeyword(keyword);
        return BaseRespVo.ok();
    }

    //搜索问题
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    @GetMapping("issue/list")
    public BaseRespVo issueList(SearchIssueBO searchIssueBO){
        IssueVO issueVO = mallManagementService.getIssue(searchIssueBO);
        return BaseRespVo.ok(issueVO);
    }

    //编辑问题
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    @PostMapping("issue/update")
    public BaseRespVo updateIssue(@RequestBody Issue issue){
        mallManagementService.updateIssue(issue);
        return BaseRespVo.ok(issue);
    }

    //删除问题:逻辑删除
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    @PostMapping("issue/delete")
    public BaseRespVo deleteIssue(@RequestBody Issue issue){
        mallManagementService.deleteIssue(issue);
        return BaseRespVo.ok();
    }

    //新增问题
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    @PostMapping("issue/create")
    public BaseRespVo createIssue(@RequestBody Issue issue){
        mallManagementService.createIssue(issue);
        return BaseRespVo.ok(issue);
    }

    @GetMapping("order/list")
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    public BaseRespVo orderList(OrderBO orderBO){
        OrderVo orderVo = mallManagementService.query(orderBO);
        return BaseRespVo.ok(orderVo);
    }

    @GetMapping("order/detail")
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    public BaseRespVo orderDetail(Integer id){
        OrderListVo orderListVo = mallManagementService.selectDetail(id);
        return BaseRespVo.ok(orderListVo);
    }

    @PostMapping("order/refund")
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    public BaseRespVo orderRefund(@RequestBody OrderRefundBO orderRefundBO){
        return BaseRespVo.fail("订单退款失败");
    }

    @PostMapping("order/ship")
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    public BaseRespVo orderFund(@RequestBody @Validated OrderShipBO orderShipBO){
        mallManagementService.updateOrderFund(orderShipBO);
        return BaseRespVo.ok();
    }

    //行政区域
    @RequestMapping("region/list")
    public BaseRespVo showRegion() {
        List<ShowRegionVO> showRegionVOS = mallManagementService.showRegion();
        BaseRespVo ok = BaseRespVo.ok(showRegionVOS);
        return ok;
    }

    //品牌制造商List
    @RequestMapping("brand/list")
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    public BaseRespVo brandInfo(BrandInfoBO brandInfoBO) {
        BrandInfoVO brandInfoVO = mallManagementService.getBrandInfo(brandInfoBO);
        BaseRespVo ok = BaseRespVo.ok(brandInfoVO);
        return ok;
    }

    @RequestMapping("brand/create")
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    public BaseRespVo createBrand(@RequestBody @Validated CreateBrandBO createBrandBO) {
        CreateBandVO createBandVO = mallManagementService.createBrand(createBrandBO);
        BaseRespVo ok = BaseRespVo.ok(createBandVO);
        return ok;
    }

    @RequestMapping("brand/update")
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    public BaseRespVo updateBrand(@RequestBody @Validated CreateBrandBO createBrandBO) {


        UpdateBrandVO updateBrandVO = mallManagementService.updateBrand(createBrandBO);

        BaseRespVo ok = BaseRespVo.ok(updateBrandVO);
        return ok;
    }

    @RequestMapping("brand/delete")
    @RequiresRoles(value = {"超级管理员", "商场管理员"}, logical = Logical.OR)
    public BaseRespVo deleteBrand(@RequestBody BrandInfo brandInfo) {
        Integer id = brandInfo.getId();
        mallManagementService.deleteBrand(id);
        return BaseRespVo.ok();
    }
}
